package com.hupu.jrs.data

import android.text.TextUtils
import com.hupu.jrs.data.local.ContentLocalDataSource
import com.hupu.jrs.data.remote.ContentRemoteDataSource
import com.hupu.jrs.db.ThreadInfo
import com.hupu.jrs.db.ThreadReply
import com.hupu.jrs.util.HtmlUtil
import rx.Observable
import javax.inject.Inject

/**
 * Created by sll on 2016/6/3.
 */
class ContentRepository @Inject
constructor(private val mContentRemoteDataSource: ContentRemoteDataSource,
            private val mContentLocalDataSource: ContentLocalDataSource) : ContentDataSource {

    override fun getThreadInfo(fid: String, tid: String): Observable<ThreadInfo> {
        val remote = mContentRemoteDataSource.getThreadInfo(fid, tid)
        val local = mContentLocalDataSource.getThreadInfo(fid, tid)

        val remoteWithLocalUpdate = remote.doOnNext { threadInfo ->
            if (threadInfo != null && threadInfo!!.error == null) {
                threadInfo.forumName = threadInfo.forum!!.name
                mContentLocalDataSource.saveThreadInfo(threadInfo)
            }
        }
        return Observable.concat(remoteWithLocalUpdate, local).first { threadInfo -> threadInfo != null }
    }

    override fun getReplies(fid: String, tid: String, page: Int): Observable<List<ThreadReply>> {
        val remote = mContentRemoteDataSource.getReplies(fid, tid, page)
        val local = mContentLocalDataSource.getReplies(fid, tid, page)

        val remoteWithLocalUpdate = remote.doOnNext { threadReplies ->
            if (threadReplies != null) {
                for (reply in threadReplies) {
                    reply.tid = tid
                    reply.isLight = false
                    reply.pageIndex = page
                    saveReply(reply)
                }
            }
        }

        return Observable.concat(remoteWithLocalUpdate, local)
                .first { threadReplies -> threadReplies != null }
    }

    override fun getLightReplies(fid: String, tid: String): Observable<List<ThreadReply>> {
        val remote = mContentRemoteDataSource.getLightReplies(fid, tid)
        val local = mContentLocalDataSource.getLightReplies(fid, tid)

        val remoteWithLocalUpdate = remote.doOnNext { threadReplies ->
            if (threadReplies != null) {
                for (reply in threadReplies) {
                    reply.tid = tid
                    reply.isLight = true
                    saveReply(reply)
                }
            }
        }
        return Observable.concat(remoteWithLocalUpdate, local)
                .first { threadReplies -> threadReplies != null }
    }

    private fun saveReply(reply: ThreadReply) {
        if (!reply.quote!!.isEmpty()) {
            val quote = reply.quote!![0]
            reply.quoteHeader = quote.header!!.get(0)
            if (!TextUtils.isEmpty(quote.togglecontent)) {
                reply.quoteToggle = quote.togglecontent
            }
            reply.quoteContent = HtmlUtil.transImgToLocal(quote.content!!)
        }
        mContentLocalDataSource.saveThreadReply(reply)
    }
}
